package com.sjzsqjy.asdtiang.action;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.SearchService;
import com.sjzsqjy.asdtiang.mysql.SampleResult;
import com.sjzsqjy.asdtiang.mysql.SampleResultChar;
import com.sjzsqjy.asdtiang.mysql.SampleType;
import com.sjzsqjy.asdtiang.mysql.SampleUser;
import com.sjzsqjy.asdtiang.search.SampleResultFormat;

public class SampleSearchAction extends ActionSupport {
	private Log log = LogFactory.getLog(ShiQiaoSearchAction.class);

	private SearchService searchService;
	private List resultShow;
	private SampleUser sampleUser;
	private String resultDate;
	private String sampleId;
	private String date;

	public String execute() throws Exception {
		List sampleResult = null, sampleResultChar = null;
		sampleUser = (SampleUser) ActionContext.getContext().getSession().get(
				"sampleUser");
		if (null == sampleUser) {
			log.info("date" + date + "dampleId:" + sampleId);
			// sampleUser=searchService.sampleUserSearch1(date, sampleId);
			// l = searchService.sampleResult(date,sampleId);
		} else {
			sampleResult = searchService.sampleResultList(sampleUser
					.getSampleId());
			sampleResultChar = searchService.sampleResultCharList(sampleUser
					.getSampleId());
		}
		List<SampleResultFormat> result = new ArrayList<SampleResultFormat>();
		SampleResultFormat format = null;
		SampleType type = null;
		SampleResult sample = null;
		SampleResultChar sampleChar = null;
		for (int i = 0; i < sampleResult.size(); i++) {
			sample = (SampleResult) sampleResult.get(i);
			format = new SampleResultFormat();
			type = searchService.sampleType(sample.getName());
			format.setSequence(type.getSequence());
			format.setCode(sample.getName());
			format.setName(type.getName());
			format.setUnit(type.getUnit());
			format.setValue(sample.getValue());
			System.out.println(sample.getValue()+"setValue");
			format.setLow(Float.parseFloat(type.getLow()));
			format.setHight(Float.parseFloat(type.getHight()));
			format.normal();
			result.add(format);
		}
		if (null != sampleResultChar) {
			for (int i = 0; i < sampleResultChar.size(); i++) {
				format = new SampleResultFormat();
				sampleChar = (SampleResultChar) sampleResultChar.get(i);
				format.setCode(sampleChar.getItem());
				format.setName(sampleChar.getName());
				format.setUnit(sampleChar.getUnit());
				try {
					format.setValue(sampleChar.getValue());
					format.setLow(Float.parseFloat(sampleChar.getLow()));
					format.setHight(Float.parseFloat(sampleChar.getHight()));
					format.setSequence(11);
					format.normal();
				} catch (NumberFormatException e) {
					format.setValue(sampleChar.getValue());
					format.setSequence(13);
				}
				result.add(format);
			}
		}
		Iterator<SampleResultFormat> iterator = result.iterator();
		List<SampleResultFormat> list = new ArrayList<SampleResultFormat>();
		int ldl_c = 0;// /标记是否有LDL_C
		int ggt = 0;// 标记是否是肝功能12，为0表示没有
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		while (iterator.hasNext()) {
			format = (SampleResultFormat) iterator.next();
			list.add(format);
			if (format.getCode().equals("TP")) {
				float alb = getItemValue("ALB",
						(ArrayList<SampleResultFormat>) result);
				System.out.println(alb+"alb");
				float tp = getItemValue("TP",
						(ArrayList<SampleResultFormat>) result);
				System.out.println(tp+"tp");
				type = searchService.getSampleTypeByItem("A/G");
				SampleResultFormat format1 = new SampleResultFormat();
				format1.setValue(nf.format(alb / (tp - alb)));
				System.out.println(alb+"albgg"+type.getName());
				list.add(getFormat(format1, type));
			}
			if (format.getCode().equals("LDL_C")) {
				ldl_c = 1;// 有LDL_C
			}
			if (format.getCode().equals("GGT")) {
				ggt = 1;// 有LDL_C
			}
		}
		if (ldl_c == 0) {// //没有LDL_C
			if (getItemValue("CHO", (ArrayList<SampleResultFormat>) result) != null
					&& getItemValue("TG",
							(ArrayList<SampleResultFormat>) result) != null
					&& getItemValue("HDL_C",
							(ArrayList<SampleResultFormat>) result) != null) {
				float cho = getItemValue("CHO",
						(ArrayList<SampleResultFormat>) result);
				float tg = getItemValue("TG",
						(ArrayList<SampleResultFormat>) result);
				float hdl_c = getItemValue("HDL_C",
						(ArrayList<SampleResultFormat>) result);
				type = searchService.getSampleTypeByItem(("LDL_C"));
				format = new SampleResultFormat();
				format.setValue(nf.format(cho - tg / 2.2 - hdl_c));
				if(!format.getValue().equals("0")){
				    list.add(getFormat(format, type));
				}
			}
		}
		if (ggt == 1) {
			if (getItemValue("ALB", (ArrayList<SampleResultFormat>) result) != null
					&& getItemValue("TP",
							(ArrayList<SampleResultFormat>) result) != null) {
				float alb = getItemValue("ALB",
						(ArrayList<SampleResultFormat>) result);
				float tp = getItemValue("TP",
						(ArrayList<SampleResultFormat>) result);
				type = searchService.getSampleTypeByItem(("GLB"));
				format = new SampleResultFormat();
				format.setValue(nf.format(tp - alb));
				if(!format.getValue().equals("0")){
				    list.add(getFormat(format, type));
				}
			}
			if (getItemValue("TBIL", (ArrayList<SampleResultFormat>) result) != null
					&& getItemValue("DBIL",
							(ArrayList<SampleResultFormat>) result) != null) {
				float tbil = getItemValue("TBIL",
						(ArrayList<SampleResultFormat>) result);
				float dbil = getItemValue("DBIL",
						(ArrayList<SampleResultFormat>) result);
				type = searchService.getSampleTypeByItem(("IBiL"));
				format = new SampleResultFormat();
				format.setValue(nf.format(tbil - dbil));
				if(!format.getValue().equals("0")){
				    list.add(getFormat(format, type));
				}
			}
		}
		if(sampleUser.getAge().equals("null")){
			sampleUser.setAge("");
		}
		resultShow = list;
		resultDate = formatDate(sampleUser.getSampleId());
		Collections.sort(resultShow, new Comparator() {
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				return ((SampleResultFormat) o1).getSequence()
						- ((SampleResultFormat) o2).getSequence();
			}
		});
		return SUCCESS;
	}

	private static String formatDate(String date) {
		return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
				+ date.substring(6, 8);
	}

	private Float getItemValue(String item, ArrayList<SampleResultFormat> list) {
		for (SampleResultFormat l : list) {
			if (l.getCode().equals(item)) {
				if (!l.getValue().equals("")) {
					return Float.parseFloat(l.getValue());
				} else {
					return 0.0f;
				}
			}
		}
		return 0.0f;
	}

	private SampleResultFormat getFormat(SampleResultFormat format,
			SampleType type) {
		format.setLow(Float.parseFloat(type.getLow()));
		format.setHight(Float.parseFloat(type.getHight()));
		format.setSequence(11);
		format.setCode(type.getItem());
		format.setName(type.getName());
		format.setUnit(type.getUnit());
		format.setSequence(type.getSequence());
		format.normal();
		return format;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public List getResultShow() {
		return resultShow;
	}

	public void setResultShow(List resultShow) {
		this.resultShow = resultShow;
	}

	public SampleUser getSampleUser() {
		return sampleUser;
	}

	public void setSampleUser(SampleUser sampleUser) {
		this.sampleUser = sampleUser;
	}

	public String getResultDate() {
		return resultDate;
	}

	public void setResultDate(String resultDate) {
		this.resultDate = resultDate;
	}

	public String getSampleId() {
		return sampleId;
	}

	public void setSampleId(String sampleId) {
		this.sampleId = sampleId;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

}
